package com.example.cyh.bczapp.Code.BLL;

import com.example.cyh.bczapp.Code.Helper.HttpHelper;
import com.example.cyh.bczapp.Code.Model.Tran;
import com.example.cyh.bczapp.Code.Model.WordInfEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;

/**
 * Created by cyh on 2017/6/9.
 */

public class WordBLL {
    private final String BaseUrl = "http://123.207.86.30/public/index.php/db";

    public ArrayList<WordInfEntity> IdGetRecord(int[] id) {
        Dictionary<String, String> param = new Hashtable();
        param.put("id", String.valueOf(id));
        try {
            String restult = HttpHelper.POST(BaseUrl + "/Wordcontrol/UserLogin", param);
            if (restult == "id||pwd error") {
                return null;
            }
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<WordInfEntity>>() {
            }.getType();
            ArrayList<WordInfEntity> list = gson.fromJson(restult, type);
            return list;
        } catch (Exception exc) {
            return null;
        }


    }

    public ArrayList<WordInfEntity> GetWordInf(ArrayList<String> idlist) {
        String str = "";
        for (String id : idlist
                ) {
            if (id == "")
                continue;
            str += id + ",";
        }
        if (str == "") {
            return new ArrayList<WordInfEntity>();
        }
        str = str.substring(0, str.length() - 1);
        Dictionary<String, String> param = new Hashtable();
        param.put("id", String.valueOf(str));
        try {
            String restult = HttpHelper.POST(BaseUrl + "/Wordcontrol/GetWordList", param);
            if (restult == "error") {
                return null;
            }
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<WordInfEntity>>() {
            }.getType();
            ArrayList<WordInfEntity> list = gson.fromJson(restult, type);
            return list;
        } catch (Exception exc) {
            return null;
        }
    }

    public String Translation(String english) {
        Dictionary<String, String> param = new Hashtable();
        param.put("keyfrom", "cyhapp");
        param.put("key", "1345740241");
        param.put("type", "data");
        param.put("doctype", "json");
        param.put("version", "1.1");
        param.put("q", english);
        try {
            Gson gson = new Gson();
            Type type = new TypeToken<Tran>() {
            }.getType();
            String restult = HttpHelper.Get("http://fanyi.youdao.com/openapi.do", param);
            Tran tran = gson.fromJson(restult, type);
            return tran.translation[0];
        } catch (Exception exc) {
            return "";
        }
    }

    public Tran FindWord(String Word) {
        Dictionary<String, String> param = new Hashtable();
        param.put("keyfrom", "cyhapp");
        param.put("key", "1345740241");
        param.put("type", "data");
        param.put("doctype", "json");
        param.put("version", "1.1");
        param.put("q", Word);
        try {
            Gson gson = new Gson();
            Type type = new TypeToken<Tran>() {
            }.getType();
            String restult = HttpHelper.Get("http://fanyi.youdao.com/openapi.do", param);
            Tran tran = gson.fromJson(restult, type);
            return tran;
        } catch (Exception exc) {
            return null;
        }
    }
}
